#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

int main()
{
    pid_t pid = fork();
    if(pid == 0)//子进程
    {   /*execl的使用
        execl("/bin/ls","ls","-l","-a",NULL);
        perror("execl");
        return 1;*/
        
        /*execlp使用
        execlp("ls","ls","-l","-a",NULL);
        perror("execlp");
        return 1;*/

        /*execlp使用
        char* env[] = {"PATH=/usr/bin","HOME=/home/usr",NULL};
        printf("execle...\n");
        execle("/bin/ls","ls","-l","-a",NULL,env);
        perror("execve");
        return 1;*/

        /*execv使用
        char* argv[] = {"/bin/ls","-l","-a",NULL};
        execv("/bin/ls",argv);
        perror("execv");
        return 1;*/

        /*execvp使用
        char* argv[] = {"ls","-l","-a",NULL};
        execvp("ls",argv);
        perror("execvp");
        return 1;*/

        /*execve使用
        char* argv1[] = {"ls","-l","-a",NULL};
        char* env[] = {"PATH=/bin/usr","HOME=/home/usr",NULL};
        execve("/bin/ls",argv1,env);
        perror("execve");
        return 1;*/

    }else if(pid > 0){//父进程
        int status = 0;
        printf("child runing......\n");
        pid_t ret = waitpid(pid,&status,0);
        
        if(WIFEXITED(status)){
            printf("child exited with pid : %d\n",ret);
        }
    }else{
        perror("fork!");
        return 1;
    }

    return 0;
}
